#!/bin/bash
# ==========================================
# 项目部署脚本 - 打包上传到服务器
# ==========================================

set -e

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 配置
REMOTE_HOST="10.1.1.69"
REMOTE_USER="root"
REMOTE_PATH="/home/yuncDataWorks"
PROJECT_NAME="flink_web_analytics"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PACKAGE_NAME="${PROJECT_NAME}_${TIMESTAMP}.tar.gz"

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}开始部署 Flink Web Analytics${NC}"
echo -e "${GREEN}========================================${NC}"

# 1. 打包项目
echo -e "\n${YELLOW}📦 步骤1: 打包项目文件...${NC}"
cd $(dirname "$0")/..

tar -czf "${PACKAGE_NAME}" \
  --exclude='node_modules' \
  --exclude='venv' \
  --exclude='backend/venv' \
  --exclude='dist' \
  --exclude='build' \
  --exclude='__pycache__' \
  --exclude='*.pyc' \
  --exclude='*.pyo' \
  --exclude='.git' \
  --exclude='.DS_Store' \
  --exclude='logs' \
  --exclude='*.log' \
  --exclude='*.pid' \
  --exclude='backend/__pycache__' \
  --exclude='frontend/node_modules' \
  --exclude='frontend/dist' \
  --exclude='.pytest_cache' \
  --exclude='.coverage' \
  --exclude='htmlcov' \
  --exclude='.vscode' \
  --exclude='.idea' \
  --exclude='*.swp' \
  --exclude='*.swo' \
  "${PROJECT_NAME}/"

if [ $? -eq 0 ]; then
    PACKAGE_SIZE=$(du -h "${PACKAGE_NAME}" | cut -f1)
    echo -e "${GREEN}✅ 打包完成: ${PACKAGE_NAME} (${PACKAGE_SIZE})${NC}"
else
    echo -e "${RED}❌ 打包失败${NC}"
    exit 1
fi

# 2. 上传到服务器
echo -e "\n${YELLOW}📤 步骤2: 上传到服务器...${NC}"
scp "${PACKAGE_NAME}" ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 上传完成${NC}"
else
    echo -e "${RED}❌ 上传失败${NC}"
    rm -f "${PACKAGE_NAME}"
    exit 1
fi

# 3. 在服务器上解压
echo -e "\n${YELLOW}📂 步骤3: 解压文件...${NC}"
ssh ${REMOTE_USER}@${REMOTE_HOST} << EOF
    cd ${REMOTE_PATH}
    
    # 备份旧版本（如果存在）
    if [ -d "${PROJECT_NAME}" ]; then
        echo "备份旧版本..."
        mv ${PROJECT_NAME} ${PROJECT_NAME}_backup_${TIMESTAMP}
    fi
    
    # 解压新版本
    echo "解压新版本..."
    tar -xzf ${PACKAGE_NAME}
    
    # 删除压缩包
    rm -f ${PACKAGE_NAME}
    
    echo "✅ 解压完成"
    echo "项目路径: ${REMOTE_PATH}/${PROJECT_NAME}"
EOF

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 解压完成${NC}"
else
    echo -e "${RED}❌ 解压失败${NC}"
    rm -f "${PACKAGE_NAME}"
    exit 1
fi

# 4. 清理本地压缩包
echo -e "\n${YELLOW}🧹 步骤4: 清理本地文件...${NC}"
rm -f "${PACKAGE_NAME}"
echo -e "${GREEN}✅ 清理完成${NC}"

# 5. 显示后续操作提示
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}🎉 部署完成！${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "\n${YELLOW}📝 后续操作：${NC}"
echo -e "1. SSH登录到服务器："
echo -e "   ${GREEN}ssh ${REMOTE_USER}@${REMOTE_HOST}${NC}"
echo -e ""
echo -e "2. 进入项目目录："
echo -e "   ${GREEN}cd ${REMOTE_PATH}/${PROJECT_NAME}${NC}"
echo -e ""
echo -e "3. 配置环境变量："
echo -e "   ${GREEN}cp env.example .env && vim .env${NC}"
echo -e ""
echo -e "4. 启动Docker服务："
echo -e "   ${GREEN}docker-compose up -d${NC}"
echo -e ""
echo -e "5. 查看服务状态："
echo -e "   ${GREEN}docker-compose ps${NC}"
echo -e "   ${GREEN}docker-compose logs -f${NC}"
echo -e ""
echo -e "${YELLOW}💡 提示：旧版本已备份到 ${PROJECT_NAME}_backup_${TIMESTAMP}${NC}"
echo -e ""

